package com.bbm.communitysystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.CarLocal;
import com.bbm.communitysystem.domain.dto.CarLocalDto;
import com.bbm.communitysystem.service.ICarLocalService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-21
 */
@Tag(name = "车位")
@RestController
@RequestMapping("/car-local")
@RequiredArgsConstructor
public class CarLocalController {

    private final ICarLocalService carLocalService;

    @PostMapping
    public R createCarLocal(@RequestBody CarLocalDto carLocal) {

        CarLocal carLocal1 = BeanUtil.copyProperties(carLocal, CarLocal.class);

        carLocal1.setUpdateTime(System.currentTimeMillis() + "");

        return carLocalService.save(carLocal1) ? R.ok("创建成功") : R.error("创建失败");
    }

    @GetMapping
    public List<CarLocal> getAllCarLocals() {
        return carLocalService.list();
    }

    @GetMapping("/{id}")
    public CarLocal getCarLocalById(@PathVariable Long id) {
        return carLocalService.getById(id);
    }

    @PutMapping("/{id}")
    public R updateCarLocal(@RequestBody CarLocalDto carLocal) {

        CarLocal carLocal1 = BeanUtil.copyProperties(carLocal, CarLocal.class);

        carLocal1.setUpdateTime(System.currentTimeMillis() + "");

        return carLocalService.updateById(carLocal1) ? R.ok("修改成功") : R.error();
    }

    @DeleteMapping("/{id}")
    public boolean deleteCarLocal(@PathVariable Long id) {
        return carLocalService.removeById(id);
    }
}
